<?php
namespace app\agent\ext;

use app\common\cache\user\IUserCache;
use app\common\enum\HtmlEnumValue;
use app\common\enum\IsDelete;
use app\common\enum\IsHistory;
use app\common\ext\IDb;

class IReport {
   /**
    * 获取用户资质信息
    * @param $uid int
    * @return array;
    */
    public  static  function get_report_info($uid){
        //设置查询条件
        $where['user_id'] = $uid;
        $where['is_delete'] = IsDelete::No;
        $where['is_history'] = IsHistory::not;

        $list = [];
        //用户信息
         $list['user'] = IUserCache::getInfo($uid);
        if(empty($list['user'])){
            error('获取用户信息失败');
        }else{
            //设置用户注册时间
            $list['user']['reg_time'] = date("Y-m-d",strtotime($list['user']['reg_time']));

            //判断用户信息是否正常
            if(is_array($list['user'])){
                //设置图片路径避免生成pdf图片跨域问题
                if(!empty($list['user']['identity_front_url'])) {
                    
                    $identity_front_url = @getimagesize($list['user']['identity_front_url']);
                    $list['user']['identity_front_url'] = "data:{$identity_front_url['mime']};base64," . base64_encode(@file_get_contents($list['user']['identity_front_url']));
                    
                }
                if(!empty($list['user']['identity_back_url'])) {
                    $identity_back_url = @getimagesize($list['user']['identity_back_url']);
                    $list['user']['identity_back_url'] = "data:{$identity_back_url['mime']};base64," . base64_encode(@file_get_contents($list['user']['identity_back_url']));
                    
                }
            }

        }

        //用户企业信息
        $company_where['user_id'] = $uid;
        $company_where['is_history'] = IsHistory::not;
        $list['company'] = IDb::getInstance('user_company')->setDbWhere($company_where)->row();
        if(empty($list['company']) || $list['company'] == null ){
            $list['company'] = array();
        }else{
            if(is_array($list['company'])){
                //设置图片路径避免生成pdf图片跨域问题
                if(!empty($list['company']['licence_url'])){
                    $licence_url = @getimagesize( $list['company']['licence_url']);
                    $list['company']['licence_url'] = "data:{$licence_url['mime']};base64," . base64_encode(@file_get_contents( $list['company']['licence_url']));
                    
                }
            }
        }

        //用户房产
        $list['house'] = IDb::getInstance('user_house')->setDbWhere($where)->sel();
        if(empty( $list['house'])){
            $list['house'] = array();
        }else{
            if(is_array($list['house'])){
                foreach ($list['house'] as $key=>$data){
                    //设置图片路径避免生成pdf图片跨域问题
                    if(!empty($data['licence_url'])) {
                        $licence_url = @getimagesize($data['licence_url']);
                        $data['licence_url'] = "data:{$licence_url['mime']};base64," . base64_encode(@file_get_contents($data['licence_url']));
                        
                    }

                    $list['house'][$key] = $data;
                }
                // var_dump($list['house'][0]);
                
            }
        }

        //用户贷款记录
        $list['loan'] = IDb::getInstance('user_loan')->setDbWhere($where)->sel();
        if(empty($list['loan'])){
            $list['loan'] = array();
        }

        //获取用户车辆
        $list['car'] = IDb::getInstance('user_car')->setDbWhere($where)->sel();
        if(empty( $list['car'])){
            $list['car'] = array();
        }else{
            if(is_array($list['car'])){
                foreach ($list['car'] as $key=>$data){
                    //设置图片路径避免生成pdf图片跨域问题
                    if(!empty($data['licence_url'])) {
                        $licence_url = @getimagesize($data['licence_url']);
                        $data['licence_url'] = "data:{$licence_url['mime']};base64," . base64_encode(@file_get_contents($data['licence_url']));
                    }
                    $list['car'][$key] = $data;
                }
            }
        }

        //用户保单信息
        $list['policy'] = IDb::getInstance('user_policy')->setDbWhere($where)->sel();
        if(empty($list['policy'])){
            $list['policy'] =  array();
        }else{
            if(is_array($list['policy'])){
                foreach ($list['policy'] as $key=>$data){
                    //设置图片路径避免生成pdf图片跨域问题
                    if(!empty($data['img_url'])) {
                        $licence_url = @getimagesize($data['img_url']);
                        $data['img_url'] = "data:{$licence_url['mime']};base64," . base64_encode(@file_get_contents($data['img_url']));
                    }
                    $list['policy'][$key] = $data;
                }
            }
        }

        //用户信用卡信息
        $list['credit_card']=IDb::getInstance('user_credit_card')->setDbWhere($where)->sel();
        if(empty($list['credit_card'])){
            $list['credit_card'] = array();
        }else{
            //信用卡张数
            $list['credit_count'] = count($list['credit_card']);

            //信用卡总额，总已使用额度，单卡最高额度
            $amount = 0;
            $used_amount = 0;
            $card_max_amount = 0;
            foreach ($list['credit_card'] as $value){

                if($value['amount'] > $card_max_amount){
                    $card_max_amount = $value['amount'];
                }

                $amount += $value['amount'];
                $used_amount += $value['used_amount'];
            }
            $list['credit_total'] = $amount;
            $list['credit_used_amount'] = $used_amount;
            //单卡最高额度
            $list['credit_highest_amount'] = $card_max_amount;
        }

        //用户担保信息
        $list['user_assure'] = IDb::getInstance('user_assure')->setDbWhere($where)->sel();
        if( empty($list['user_assure'])){
            $list['user_assure'] = array();
        }

        //用户网店信息
        $list['user_shop'] = IDb::getInstance('user_shop')->setDbWhere($where)->sel();
        if( empty($list['user_shop'])){
            $list['user_shop'] = array();
        }

        //取得类型信息
        $list['polisy_type'] = HtmlEnumValue::$enum_value['policy_type'];//保单类型列表
        $list['house_type'] = HtmlEnumValue::$enum_value['house_type'];//房产类型列表
        $list['user_sex'] = HtmlEnumValue::$enum_value['sex'];//用户性别
        $list['company_type'] = HtmlEnumValue::$enum_value['company_type']; //公司类型
        $list['holder_type'] = HtmlEnumValue::$enum_value['holder_type']; //所有人
        $list['loan_type'] = HtmlEnumValue::$enum_value['loan_type'];//个人贷款类型
        return $list;
    }

}